$(document).ready(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        target:null,
        dom:{
          searchBtn:null,
          exportBtn:null,
          content:null
        },
        params:{
            startDate:null,
            endDate:null,
            schoolId:null
        },
        init: function () {
            this.target = $('#target');

            this.dom.searchBtn = $('[name=search]');
            this.dom.exportBtn = $('[name=btnExport]');
            this.dom.content   = $('[name=content]');

            this.funObj().initParams();//初始化数据
            this.listener();
        },
        listener: function () {
            var $this = this;

            //点击搜索
            $this.dom.searchBtn.click(function () {
                $this.funObj().search();
            });

            //点击导出
            $this.dom.exportBtn.click(function () {
                $this.funObj().exportExcel();
            });

        },
        funObj:function () {
            var $this = this;
            return {
                search: function () {
                    $this.funObj().initParams();
                    startCache($this.params.schoolId,$this.params.startDate,$this.params.endDate);
                },
                initParams:function () {
                    $this.params.startDate = $('[name=startdate]').val();
                    $this.params.endDate   = $('[name=enddate]').val();
                    $this.params.schoolId  = xhTool.getQuery('schoolid');
                },
                exportExcel:function () {
                    $('#formexport').submit();
                }
            };
        }
    }
};
var timer;
function startCache (schoolId, startTime, endTime) {
    window.top.swal({
        title: '获取数据执行中！请耐心等待...',
        showConfirmButton: false,
        type: 'success',
        allowOutsideClick: false
    });
    setTimeout(function () {
        window.top.swal.close();
    }, 5*60 * 1000);
    xhExtra.ajaxRequest('stats_brush/startCacheBrush', {schoolId: schoolId, startDate: startTime, endDate: endTime}, {check: false, timeout: 2}, function (data) {
        if (typeof data === 'object' && data.error) {
            window.top.xhTop.showError(data.error);
            window.top.swal.close();
            return false;
        }
        timer = setInterval(function () {
            checkCache(schoolId, startTime, endTime);
        }, 1000 * 5);
    });
}
function checkCache (schoolId, startTime, endTime) {
    xhExtra.ajaxRequest('stats_brush/checkBrushCache', {schoolId: schoolId, startDate: startTime, endDate: endTime}, {check: false}, function (data) {
        if (data.code === 200) {
            clearInterval(timer);
            window.top.swal.close();
            xhExtra.ajaxRequest('stats_brush/search', {schoolId: schoolId, startDate: startTime, endDate: endTime}, {check: true}, function (data) {
                initStr(data)
            });
        }
    });

}

function initStr(data) {
    var str = '';
    $.each(data,function (i,item) {
        str+='<table>';
        str+='  <tr><td height="30" colspan="11" style="color:#fff;background-color:#3399FF;text-align: left; ">班级：'+item['className']+'&nbsp;&nbsp;&nbsp;&nbsp;教师:'+item['tchName']+'&nbsp;&nbsp;&nbsp;&nbsp; 学生数目：'+item['stuNum']+'&nbsp;&nbsp;&nbsp;&nbsp; '+$('[name=startdate]').val()+' 至 '+$('[name=enddate]').val()+'</td></tr>';
        str+='  <tr height="26" style="background-color:#ececec;"><td>姓名</td><td>等级</td><td>总做题篇数</td><td>总正确率</td><td>总满分篇数</td><td>作业篇数</td><td>作业正确率</td><td>作业满分篇数</td><td>自练篇数</td><td>自练正确率</td><td>自练满分篇数</td></tr>';
        $.each(item['students'],function (i,stu) {
            var totalExec  = stu['practiceNum'] + stu['practiceSelfNum'];
            var totalRight = (((stu['practiceSelfRightScore'] + stu['rightScore']) / (stu['practiceSelfAllScore'] + stu['allScore']))*100);
            if (isNaN(totalRight)) {
                totalRight = 0;
            }
            if (totalRight !== 0 ) {
                totalRight = totalRight.toFixed(2);
            }
            var totalFull  = stu['parcFullNum'] + stu['practiceSelfFullNum'];
            str+='  <tr style="background-color:#ffffff;"><td>'+stu['stuName']+'</td><td>'+stu['brushLevel']+'</td><td>'+totalExec+'</td><td>'+totalRight+'%</td><td>'+totalFull+'</td><td>'+stu['practiceNum']+'</td><td>'+(stu['pracRightRate'] * 100).toFixed(2)+'%</td><td>'+stu['parcFullNum']+'</td><td>'+stu['practiceSelfNum']+'</td><td>'+(stu['practiceSelfRightRate'] * 100).toFixed(2)+'</td><td>'+stu['practiceSelfFullNum']+'</td></tr>';
        });
        str+='</table>';
    });
    $('#target').append(str);
    $('[name=content]').val(str);
}